﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Core.Repository.MasterDataRepositories;
using Icoders.MarketTracker.Web.Lib.Response.Containers;

namespace Icoders.MarketTracker.Web.Lib.Response.Services.Impl
{
   public class CreateApplicationService : ICreateApplicationService
   {
       private IApplicationRepository _applicationRepository;
       private IUserRepository _userRepository;
       private IFrsAssignRepository _frsAssignRepository;

       public CreateApplicationService(IApplicationRepository applicationRepository, IUserRepository userRepository, IFrsAssignRepository frsAssignRepository)
       {
           _applicationRepository = applicationRepository;
           _userRepository = userRepository;
           _frsAssignRepository = frsAssignRepository;
       }

       public CreateApplicationResponse CreateApplication(string username, string password)
       {
          bool isactive= _userRepository.IsLoginMobile(username, password);
          if (!isactive)
          {
              return new CreateApplicationResponse { ErrorInfo = "Invalid username and Password" };
          }
          else
          {
              User user = _userRepository.GetByUsername(username);
              MApplication app = new MApplication {UserId = user.Id};
              var assign = _frsAssignRepository.GetByUser(user.Id);
              if(assign==null)
              {
                 return new CreateApplicationResponse { ErrorInfo = "You have not been assigned to an area. Contact you Manager" }; 
              }
              int clientId = assign != null ? assign.ClientId : 0;
              int appid = _applicationRepository.Save(app);
              return new CreateApplicationResponse { ErrorInfo = "Success", ApplicationId = appid, UserId = user.Id, ClientId = clientId };


          }

       }
    }
}
